package com.uchengwang.ui.cm_ui.uu.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.uchengwang.R;
import com.uchengwang.ui.cm_ui.ucity.presenter.HttpCityPresenter;
import com.uchengwang.ui.cm_ui.uu.bean.CmtmsgBean;
import com.uchengwang.ui.self.circledemo.spannable.SpannableClickable;
import com.uchengwang.ui.utils.UrlUtils;
import com.uchengwang.ui.utils.DateUtil;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by U城网 on 2017/8/17.
 */

public class CmtmsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private Context context;
    private List<CmtmsgBean> list = new ArrayList<>();

    public CmtmsgAdapter(Context context){
        this.context = context;
    }

    public void setData(List<CmtmsgBean> list){
        this.list = list;
        notifyDataSetChanged();
    }

    public List<CmtmsgBean> getData(){
        return list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_cmt_msg, parent, false);
        return new CmtmsgVH(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder h, int position) {
        CmtmsgVH holder = (CmtmsgVH) h;

        final CmtmsgBean bean = list.get(position);
        final int type = bean.getUnreadType();
        holder.cmtMsg_time.setText(DateUtil.getDateUtil().getTimeStringDate(bean.getCreateTime()));

        SpannableStringBuilder builder = new SpannableStringBuilder();

        String nickName = bean.getFromNickName();
        if (!TextUtils.isEmpty(nickName)) {
            builder.append(setClickableSpan(nickName, bean.getFromUid()));
            builder.append(" 回复 ");
            builder.append("你");
            builder.append(": ");
        }
        //转换表情字符
        final String contentBodyStr = bean.getUnreadMessage();
        builder.append(UrlUtils.formatUrlString(contentBodyStr));
        holder.cmtMsg_content.setText(builder);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HttpCityPresenter.getPresenter().ucityOnClick((Activity) context, bean.getPubId());

            }
        });




    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class CmtmsgVH extends RecyclerView.ViewHolder{
        private View itemView;
        private TextView cmtMsg_time, cmtMsg_content;

        public CmtmsgVH(View view) {
            super(view);

            AutoUtils.autoSize(view);
            itemView = view;

            cmtMsg_time = (TextView) view.findViewById(R.id.cmtMsg_time);
            cmtMsg_content = (TextView) view.findViewById(R.id.cmtMsg_content);

        }
    }

    @NonNull
    private SpannableString setClickableSpan(final String textStr, final long id) {
        SpannableString subjectSpanText = new SpannableString(textStr);
        subjectSpanText.setSpan(new SpannableClickable(Color.parseColor("#588BF5")){
                                    @Override
                                    public void onClick(View widget) {
                                        Toast.makeText(context, textStr + " &id = " + id, Toast.LENGTH_SHORT).show();
                                    }
                                }, 0, subjectSpanText.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return subjectSpanText;
    }
}
